import moment from 'moment';

// 如(22,22)==>00:30
export const px2Time = (px, minUnit) => {
    // if (!px) return '';
    const t = 30 * Math.round(px / minUnit);
    const h = Math.floor(t / 60);
    const m = t % 60;
    return `${h < 10 ? '0' : ''}${h}:${m < 10 ? '0' : ''}${m}`;
};

// 如(00:30,22)==>22
export const time2px = (time, minUnit) => {
    if (!time) return '';
    const [h, m] = time.split(':') || [];
    const t = Number.parseInt(h, 10) * 2 + Number.parseInt(m, 10) / 30;
    return t * minUnit;
};

export const checkIntersection = (a, b) => {
    const min = a[0] < b[0] ? a : b;
    const max = min === a ? b : a;
    return min[1] > max[0];
};

export const formatHM = (time, unit) => {
    if (!time) return '';
    return time2px(moment(time).format('HH:mm'), unit);
};

export const formatStaticMap = (obj, unit) => {
    if (!obj) return null;
    const fObj = {};
    const keys = Object.keys(obj);
    keys.forEach(item => {
        const arr = obj[item];
        if (arr && arr.length > 0) {
            const fArr = arr.map((it, i) => ({
                id: i + 1,
                range: [formatHM(it[0], unit), formatHM(it[1], unit)]
            }));
            fObj[item] = fArr;
        }
    });
    return fObj;
};
